<?php

namespace App\Controller\LazyCommon;

use App\Base\BaseController4Admin;
use App\Model\LazyCommon\AdminMenuModel;
use App\Model\LazyCommon\UserModel;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 * @Route("/admin")
 */
class AdminDefaultController extends BaseController4Admin
{

    /**
     * 后台入口
     *
     * @Route("/index",name="AdminLogin")
     */
    public function AdminLogin()
    {
        $data = array();
        return $this->render("LazyCommon/login.ele.html.twig",$data);
    }


    /**
     * 后台登录请求
     *
     * @param Request $request
     * @Route("/login",name="AdminLoginDo")
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function AdminLoginDo(Request $request)
    {
        $userName =  $this->getParamRequired("username");
        $password =  $this->getParamRequired("password");
        $password = md5($password);
        $UserModel = new UserModel($this->getDoctrine());
        $user = $UserModel->commLogin($userName,$password,$request);
        if ($user->getGroups() == 999) {
            $user->setGroups(1);
        }

        $sessData = $UserModel->createSessionData($user);
        $this->setSessionData($sessData);
        if ($user) {
            $this->setSessionData($UserModel->createSessionData($user));
            return $this->redirectToRoute("adminHomePage");
        } else {
            return $this->redirectToRoute("AdminLogin");
        }
    }




    /**
     * 后台外层框架主页
     * @Route("/main/home",name="adminHomePage")
     */
    public function homeAction()
    {
        $uid = $this->needToBeAdmin();
        $data["rnd"] = "?rnd=".uniqid();
        $AdminMenuModel = new AdminMenuModel($this->getDoctrine());
        if ($uid == 1) {
            $menu = $AdminMenuModel->getAdminListsJson();
        }else{
            $menu = $AdminMenuModel->getAdminMenu4User($uid);
        }
        $data["menu"] = $menu;
        return $this->render("LazyCommon/index.html.twig",$data);
    }

    /**
     * 后台左侧按钮
     * @Route("/main/sidebar",name="adminSidebarPage")
     */
    public function sidebarAction()
    {
        return $this->render("LazyCommon/sidebar.ele.php.twig");
    }

    /**
     * 后台登陆处理
     * @Route("/main/logout/do",name="adminLogout")
     */
    public function logoutAction(Request $request)
    {
        $session = $request->getSession();
        $session->clear();
        return $this->redirectToRoute("AdminLogin");
    }

    /**
     * 后台修改密码页面
     * @Route("/main/mod/password",name="modAdminPasswordPage")
     */
    public function modPasswordAction()
    {
        $title = "修改密码";
        $data = array(
            "title"   => $title,
            "submitUrl"   => $this->generateUrl("doModAdminPassword"),
            "submitType"   => "GET",
        );
        return $this->render("LazyCommon/modPassword.php.twig",$data);
    }

    /**
     * 后台修改密码处理
     * @Route("/main/mod/password/do",name="doModAdminPassword")
     */
    public function doModPasswordAction()
    {
        if ($oldpsw = $this->getParamRequired("oldpsw")){} else {return $this->js_out("请输入原密码",$this->generateUrl("modAdminPasswordPage"));}
        if ($newpsw = $this->getParamRequired("newpsw")){} else  {return $this->js_out("请输入新密码",$this->generateUrl("modAdminPasswordPage"));}
        if ($newpswag = $this->getParamRequired("newpswag")){} else  {return $this->js_out("请输入确认新密码",$this->generateUrl("modAdminPasswordPage"));}

        if ($newpsw != $newpswag) {
            return $this->js_out('两次输入的密码不一致，请刷新后重试！',$this->generateUrl("modAdminPasswordPage"));
        }
        $id = $this->needToBeAdmin();
        $where = array(
            "id" => $id,
            "password"=>md5($oldpsw),
        );
        $DR = $this->getDoctrine()
            ->getRepository("App:User");
        $admin = $DR->findOneBy($where);

        if ($admin) {
            $DM = $this->getDoctrine()->getManager();
            $admin->setPassword( md5($newpsw));
            $DM->flush();
            return $this->js_out('修改成功',$this->generateUrl("modAdminPasswordPage"));
        }else{
            return $this->js_out('密码错误',$this->generateUrl("modAdminPasswordPage"));
        }
    }
}
